package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.SupplierService;
import com.avantis.admin.service.SysSerialNumberService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.Supplier;
import com.avantis.common.entity.query.SupplierQuery;
import com.avantis.common.msg.YResult;
import com.avantis.common.utils.Constants;
import com.avantis.common.utils.NumberGenerater;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
* @Description:    java类作用描述
* @Author:         kirago
* @CreateDate:     2019/2/13 10:13 AM
* @UpdateRemark:   修改内容
* @Version:        1.0
*/

@RestController
@RequestMapping("/supplier")
public class SupplierController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SupplierController.class);

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private SysSerialNumberService sysSerialNumberService;

    @PostMapping("/insert")
    @ApiOperation(value = "添加供应商", httpMethod = "POST", notes = "")
    public YResult insert(@RequestBody @Validated Supplier supplier) {
        YResult yResult = restProcessor(() -> {
            AdminUser adminUser = CurrentUser.get();
            supplier.setCreater(adminUser.getUsername());
            supplier.setModifier(adminUser.getUsername());
            supplier.setCreatedon(new Date());
            supplier.setModifyon(new Date());
            supplier.setSupplierCode(sysSerialNumberService.generateSerialNumberByModelCode(Constants.CUSTOMER_PRE));
            supplierService.saveSupplier(supplier);
            return YResult.ok();
        });
        return yResult;
    }

    @GetMapping("/detail/{supplierId}")
    @ApiOperation(value = "供应商详情", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "supplierId", value = "供应商ID", dataType = "string"))
    public YResult detail(@PathVariable("supplierId") String supplierId) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("supplier", supplierService.findOne(supplierId));
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/list")
    @ApiOperation(value = "供应商列表", httpMethod = "POST", notes = "")
    public YResult list(@RequestBody SupplierQuery supplierQuery) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("count", supplierService.findSupplierCriteria(supplierQuery).getTotalElements());
                resultMap.put("pageCount",supplierService.findSupplierCriteria(supplierQuery).getTotalPages());
                resultMap.put("models",supplierService.findSupplierCriteria(supplierQuery).getContent());
                resultMap.put("pageSize",supplierService.findSupplierCriteria(supplierQuery).getSize());
                resultMap.put("pageIndex",supplierService.findSupplierCriteria(supplierQuery).getNumber()+1);
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/modify")
    @ApiOperation(value = "修改供应商信息", httpMethod = "POST", notes = "")
    public YResult modify(@RequestBody @Validated Supplier supplier) {
        YResult yResult = restProcessor(() -> {
            AdminUser adminUser = CurrentUser.get();
            supplier.setModifier(adminUser.getUsername());
            supplier.setModifyon(new Date());
            supplierService.modifySupplier(supplier);
            return YResult.ok();
        });
        return yResult;
    }

    @DeleteMapping("/delete/{supplierId}")
    @ApiOperation(value = "删除供应商信息", httpMethod = "DELETE", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "supplierId",value = "供应商Id", dataType = "string"))
    public YResult delete(@PathVariable("supplierId") String supplierId) {
        YResult yResult = restProcessor(() -> {
            try {
                supplierService.deleteSupplier(supplierId);
                return YResult.ok();
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }
}
